﻿using CloudFranceTriple.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;


namespace CloudFranceTriple.Tests.Areas.Forum
{
    [TestClass]
    public class SecurityControllerTest : UnitTestBase
    {
        private Int32 _unitTestForumId = 77;
        private Int32 _unitTestRoleId = 34;
        private Int32 _unitTestRoleId2 = 35;

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Index()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Index(_unitTestForumId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Create_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Create(_unitTestForumId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Create_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var forumSecurityset = new ForumSecuritySet
            {
                ForumSet_Id = _unitTestForumId,
                webpages_Roles_RoleId = _unitTestRoleId2,
                RightLevel = 1 //Reader = 1
            };

            // Act
            var result = controller.Create(forumSecurityset) as RedirectToRouteResult;

            UnitTestSecurityForumId = forumSecurityset.ForumSet_Id;
            UnitTestSecurityRoleId = forumSecurityset.webpages_Roles_RoleId;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Edit_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Edit(_unitTestForumId, _unitTestRoleId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Edit_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var forumSecurityset = new ForumSecuritySet
            {
                ForumSet_Id = _unitTestForumId,
                webpages_Roles_RoleId = _unitTestRoleId,
                RightLevel = 2 //Reader = 1, Author = 2
            };

            // Act
            var result = controller.Edit(forumSecurityset) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_Delete_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Delete(_unitTestForumId, _unitTestRoleId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Security")]
        public void SecurityController_DeleteConfirmed_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.SecurityController();

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.DeleteConfirmed(UnitTestSecurityForumId, UnitTestSecurityRoleId) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }
    }
}
